package com.then.component.club.repository;

import android.arch.lifecycle.LiveData;
import android.support.annotation.NonNull;

import com.then.architecture.AppExecutors;
import com.then.architecture.data.dto.Resource;
import com.then.architecture.data.repository.BaseRepository;
import com.then.architecture.data.repository.SimpleDBBoundResource;
import com.then.architecture.di.AppScope;
import com.then.component.club.api.RemoteService;
import com.then.component.club.db.location.AreaDao;
import com.then.component.club.db.location.CityDao;
import com.then.component.club.db.location.CountryDao;
import com.then.component.club.db.location.ProvinceDao;
import com.then.component.club.entity.BaseLocation;


import javax.inject.Inject;

/**
 * Created by 42524 on 2018/2/8.
 */

@AppScope
public class LocationRepository extends BaseRepository<RemoteService> {

    private ProvinceDao provinceDao;

    private CityDao cityDao;

    private CountryDao countryDao;

    private AreaDao areaDao;

    @Inject
    LocationRepository(AppExecutors appExecutors, RemoteService remoteService,
                       ProvinceDao provinceDao,
                       CityDao cityDao,
                       CountryDao countryDao,
                       AreaDao areaDao) {
        super(appExecutors, remoteService);
        this.provinceDao = provinceDao;
        this.cityDao = cityDao;
        this.countryDao = countryDao;
        this.areaDao = areaDao;
    }


    public LiveData<Resource<BaseLocation>> queryProvinceById(String provinceId) {
        return new SimpleDBBoundResource<BaseLocation, BaseLocation>(appExecutors) {
            @NonNull
            @Override
            protected LiveData<BaseLocation> loadFromDb() {
                return provinceDao.queryProvinceById(provinceId);
            }
        }.asLiveData();
    }


    public LiveData<Resource<BaseLocation>> queryCityById(String city) {
        return new SimpleDBBoundResource<BaseLocation, BaseLocation>(appExecutors) {
            @NonNull
            @Override
            protected LiveData<BaseLocation> loadFromDb() {
                return cityDao.queryCityById(city);
            }
        }.asLiveData();
    }


    public LiveData<Resource<BaseLocation>> queryAreaById(String areaId) {

        return new SimpleDBBoundResource<BaseLocation, BaseLocation>(appExecutors) {

            @NonNull
            @Override
            protected LiveData<BaseLocation> loadFromDb() {
                return areaDao.queryAreaById(areaId);
            }

        }.asLiveData();
    }

    public LiveData<Resource<BaseLocation>> queryCountryById(String countryId) {

        return new SimpleDBBoundResource<BaseLocation, BaseLocation>(appExecutors) {

            @NonNull
            @Override
            protected LiveData<BaseLocation> loadFromDb() {
                return countryDao.queryCountryById(countryId);
            }

        }.asLiveData();
    }




}
